package com.mowh.gof23.structural.patterns.proxy;

/**
 * @author mowh1gd
 * @create 2023/10/27 10:31
 */
public class PrinterProxy implements Printable{
    private String name;
    private Printer real;

    public PrinterProxy() {
    }
    public PrinterProxy(String name) {
        this.name = name;
    }

    @Override
    public synchronized void setPrinterName(String name) {
        if (real != null) {
            real.setPrinterName(name);
        }
        this.name = name;
    }

    @Override
    public String getPrintName() {
        return name;
    }

    @Override
    public void print(String aString) {
        realize();
        real.print(aString);
    }
    private synchronized void realize() {
        if (real == null) {
            real = new Printer(name);
        }
    }
}
